package com.mr.sell.controller;

import com.mr.sell.entity.ProductCategory;
import com.mr.sell.entity.ProductInfo;
import com.mr.sell.exception.CustomException;
import com.mr.sell.form.ProductForm;
import com.mr.sell.service.CategoryService;
import com.mr.sell.service.ProductService;
import com.mr.sell.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import sun.security.util.KeyUtil;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Class: SellerPrioductController
 * User (作者):MRui
 * TODO (描述)：买家端商品
 * Date 2018.09.30 22:52
 */
@RestController
@Slf4j
@RequestMapping("/seller/product")
public class SellerProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private CategoryService categoryService;
    /**
    * User (作者): MRui
    * TODO (描述)：卖家端商品列表
    * Data：2018.09.30 22:55
    */
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page",defaultValue = "1") int page,
                             @RequestParam(value = "size",defaultValue = "1") int size,
                             Map<String,Object> map){
        PageRequest request = PageRequest.of(page - 1, size);

        Page<ProductInfo> productList = productService.getAll(request);

        map.put("productInfoPage",productList);
        map.put("currentPage",page);
        map.put("size",size);
        return new ModelAndView("product/list");
    }

    /**
    * User (作者): MRui
    * TODO (描述)：上架
    * Data：2018.09.30 23:37
    */
    @GetMapping("/on_sale")
    public ModelAndView onSale(@RequestParam("productId") String productId,
                               Map<String,Object> map){
        try {
            productService.onSale(productId);
        }catch (CustomException e){
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/product/list");
            return new ModelAndView("common/error",map);
        }
        map.put("url","/sell/seller/product/list");
        return new ModelAndView("common/success",map);
    }
    /**
    * User (作者): MRui
    * TODO (描述)：下架
    * Data：2018.09.30 23:40
    */
    @GetMapping("/off_sale")
    public ModelAndView offSale(@RequestParam("productId") String productId,
                               Map<String,Object> map){
        try {
            productService.offSale(productId);
        }catch (CustomException e){
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/product/list");
            return new ModelAndView("common/error",map);
        }
        map.put("url","/sell/seller/product/list");
        return new ModelAndView("common/success",map);
    }

    /**
    * User (作者): MRui
    * TODO (描述)：新增
    * Data：2018.10.01 22:14
    */
    @GetMapping("/toAddOrUpdate")//productId非必传required = false
    public ModelAndView toAddOrUpdate(@RequestParam(value = "productId",required = false) String productId,
                      Map<String,Object> map){
        if(!StringUtils.isEmpty(productId)){
            ProductInfo productInfo = productService.getOne(productId);
            map.put("productInfo",productInfo);
        }
        //查询类目
        List<ProductCategory> productCategoryList = categoryService.getAll();
        map.put("categoryList",productCategoryList);

        return new ModelAndView("product/index");
    }
    /**
    * User (作者): MRui
    * TODO (描述)：保存
    * Data：2018.10.01 22:49
    */
    @PostMapping("/save")
    public ModelAndView save(@Valid ProductForm form, BindingResult bindingResult,Map<String,Object> map){
        if (bindingResult.hasErrors()){
            map.put("msg",bindingResult.getFieldError().getDefaultMessage());
            map.put("url","/sell/seller/product/index");
            return new ModelAndView("common/error",map);
        }
        try {
            ProductInfo productInfo = new ProductInfo();
            //如果为空说明是新增
            if (!StringUtils.isEmpty(form.getProductId())){
                productInfo = productService.getOne(form.getProductId());
            }else {
                form.setProductId(UUIDUtil.getUUID());
            }
            BeanUtils.copyProperties(form,productInfo);
            productService.save(productInfo);
        }catch (CustomException e){
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/product/index");
            return new ModelAndView("product/error",map);
        }
        map.put("url","/sell/seller/product/list");
        return new ModelAndView("common/success",map);
    }
}
